package com.system.weirdor.www.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.system.weirdor.common.advice.BaseController;
import com.system.weirdor.search.index.EsBlog;
import com.system.weirdor.search.service.EsBlogService;
import com.system.weirdor.search.service.EsCorporateService;
import com.xiaoleilu.hutool.http.Header;
import com.xiaoleilu.hutool.http.HttpRequest;
import com.xiaoleilu.hutool.lang.Console;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by weirdor on 2017/8/24.
 */
@Controller
@RequestMapping("/view")
public class WebViewController extends BaseController {

    @Autowired
    private EsCorporateService esCorporateService;

    @Autowired
    private EsBlogService esBlogService;
    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    @GetMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if(null != subject){
            subject.logout();
        }
        logger.info("您已经退出登录");
        return "redirect:/view/login";
    }
    @RequestMapping("/403")
    public String forbidden() {
        return "403";
    }


    @GetMapping("/index")
    public String index(){
        return "pages/index";
    }


    @GetMapping("/welcome")
    public String welcome(){
        return "pages/welcome";
    }



    @GetMapping("/info")
    public String info(){
        return "pages/user_info";
    }

    //这个只是测试一下
    @ResponseBody
    @GetMapping("/es")
    public Object es(){
        Map<String ,Object> paramMap=new HashMap<>();
        paramMap.put("userid","179946422@qq.com");
        paramMap.put("seckey","8c229269f53d24c5848ae2f04f412b01");
        paramMap.put("date","2017-09-30");
        paramMap.put("startStation","上海");
        paramMap.put("arriveStation","常德");
        String url="http://www.chepiao100.com/api/yupiao";
        //链式构建请求
        String result2 = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
                .form(paramMap)
                .execute().body();
        JSONObject jsonObject= JSON.parseObject(result2);
        return jsonObject;
    }

    //这个只是测试一下
    @ResponseBody
    @GetMapping("/ess")
    public Object ess(){
        EsBlog esBlog=new EsBlog();
        esBlog.setTitle("ss");
        esBlog.setId("1");
        esBlog.setContent("dsd");
        return esBlogService.save(esBlog);
    }

}
